package bai2.com.server;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.Socket;

public class MyConnection extends Thread {
	private Socket socket;
	private static int BUFFER_SIZE = 1024 * 4;

	public MyConnection(Socket socket) {
		this.socket = socket;
		start();
	}

	@Override
	public void run() {
		try {
			socket.setTcpNoDelay(true);
			InputStream inputStream = socket.getInputStream();
			ObjectInputStream objectInputStream = new ObjectInputStream(
					inputStream);
			String fileName = (String) objectInputStream.readObject();
			FileOutputStream fileOutputStream = new FileOutputStream(
					"bai_02/host/" + fileName);
			byte[] buffer = new byte[BUFFER_SIZE];
			int count = 0;
			while ((count = inputStream.read(buffer)) != -1) {
				fileOutputStream.write(buffer, 0, count);
			}
			fileOutputStream.close();
			inputStream.close();
		} catch (IOException | ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.run();
	}

}
